(function ($) {
	var sourceData;
	var perInfo,pageType,pageData,nurseid
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function submitNewData(){
		blurAllInput()
		var status = this.getAttribute('data-id');
		var parr = ''
		var domArr = pageData['parrArr']
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			var domValue = dom.getAttribute('data-id')
			if (oneDom['type'] == 'date') {
				domValue = dom.innerText
			}else if (oneDom['type'] == 'input') {
				domValue = dom.value
			}
			if (!domValue) {
				mui.toast(oneDom['tip'])
				return
			}
			parr += domValue +'^'
		}
		parr +=status+'^'
		if (sourceData) {
			parr += sourceData['rw']
		}
		//console.log(parr)
		var pdict = {'nurseid':nurseid,'parr':parr}
		var paramDict = {'method':pageData['method'],'params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('提交失败')
				return 
			}
			if (pageType == '课题专案备案' || unUploadFiles.length==0 ) {
				plus.webview.getWebviewById('../WKPlatformPages/zk/zkList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = getUploadFileToServerPath(pageType)
			var method = getSaveFileMethod(pageType)
			var paramDict={'id':msg,'path':path,'imgArr':unUploadFiles,'method':method}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/zk/zkList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		var domArr = pageData['parrArr']
		var fileKey = ''
		if (pageType == '专科活动备案') {
			fileKey = 'ActivityFile'
		}else if (pageType == '规范参与备案'){
			fileKey = 'StandardFile'
		}
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			if (oneDom['type'] == 'date') {
				dom.innerText=sourceData[oneDom['key']]
			}else if (oneDom['type'] == 'input') {
				dom.value=sourceData[oneDom['key']]
			}else{
				var value = sourceData[oneDom['key']+'Desc']
				if (oneDom['domId'] == 'persons' || oneDom['domId'] == 'StandardParticipant') {
					value = sourceData[oneDom['key']+'Name']
				}
				dom.innerText=value
				dom.setAttribute('data-id',sourceData[oneDom['key']])
			}
		}
		if (pageType == '课题专案备案') {
			return
		}
		updateFileUI(fileKey,'fileUL',sourceData,sourceData['rw'],getUploadFileToServerPath(pageType))
	}
	//级别
	function getCheckLevel(){
		blurAllInput()
		var pdict = {'nurseid':perInfo['loginID']}
		var paramDict = {'method':'web.INMQualControlComm:GetCheckLevel','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('请重试')
				return;
			}
			var arr = result.data
			if (arr == undefined) {
				arr=[{"Code":"W","Desc":"病区"},{"Code":"L","Desc":"科室"},{"Code":"H","Desc":"护理部"}]
			}
			updateOptionsUL(arr,$('#level')[0],'Code','Desc')
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	//专业组
	function getGroupList(){
		blurAllInput()
		var dom = this
		var pdict={'nurseid':nurseid}
		var paramDict = {'method':'web.INMSpecialComm:FindRoleGroupList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('获取专业组失败')
				return 
			}
			updateOptionsUL(result['data'],dom,'SubValue','SubDesc')
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function listTypeDomTapped(){
		blurAllInput()
		var textTip = this.previousElementSibling.innerText
		var tempArr
		switch (textTip){
			case '活动形式：':
				tempArr = [{value:'L',label:'主题论坛'},{value:'T',label:'主题讨论'},{value:'B',label:'专题读书报告'},
					{value:'J',label:'论坛讲座'},{value:'G',label:'专业组讲座'},{value:'O',label:'其他'}]
				break;
			case '参与形式：':
				tempArr = [{value:'P',label:'参与'},{value:'H',label:'主持'},{value:'C',label:'负责'},{value:'Z',label:'组织'},{value:'O',label:'其他'}]
				break;
			case '类型：':
				tempArr = [{value:'G',label:'规范'},{value:'D',label:'指引'},{value:'F',label:'表单'},{value:'S',label:'标准'},
					{value:'L',label:'流程'},{value:'O',label:'其他'}]
				break;
			case '制定/修订：':
				tempArr = [{value:'C',label:'制定'},{value:'U',label:'修订'}]
				break;
			case '课题/专案：':
				tempArr = [{value:'K',label:'课题'},{value:'Z',label:'专案'}]
				break;
			default:
				break;
		}
		updateOptionsUL(tempArr,$('#'+this.id)[0],'value','label')
	}
	function getSaveParams(){
		var method ='web.INMSpecialComm:SaveActivity'
		//病区Id^护士Id^活动形式^参与形式^级别^活动日期^主题^地点^状态^id
		var domArr = [{'domId':'Ward','tip':'请选择病区','key':'ActivityWard'},
			{'domId':'persons','tip':'请选择参与人','key':'ActivityNurse'},
			{'domId':'ActivityType','tip':'请选择活动形式','key':'ActivityType'},
			{'domId':'ParType','tip':'请选择参与形式','key':'ActivityParType'},
			{'domId':'level','tip':'请选择级别','key':'ActivityLevel'},
			{'domId':'date','tip':'请选择日期','type':'date','key':'ActivityDate'},
			{'domId':'Topic','tip':'请输入活动主题','type':'input','key':'ActivityTopic'},
			{'domId':'Location','tip':'请输入活动地点','type':'input','key':'ActivityLocation'}]
		if (pageType == '课题专案备案') {
			method ='web.INMSpecialComm:SaveCase'
			//病区Id^护士Id^课题专案^参与形式^级别^开始日期^结束日期^主题^所做工作^状态^id
			domArr = [{'domId':'Ward','tip':'请选择病区','key':'CaseWard'},
				{'domId':'persons','tip':'请选择参与人','key':'CaseNurse'},
				{'domId':'CaseType','tip':'请选择课题/专案','key':'CaseType'},
				{'domId':'ParType','tip':'请选择参与形式','key':'CaseParType'},
				{'domId':'level','tip':'请选择级别','key':'CaseLevel'},
				{'domId':'CaseStDate','tip':'请选择开始日期','type':'date','key':'CaseStDate'},
				{'domId':'CaseEndDate','tip':'请选择结束日期','type':'date','key':'CaseEndDate'},
				{'domId':'CaseTopic','tip':'请输入主题','type':'input','key':'CaseTopic'},
				{'domId':'CaseWork','tip':'请输入所做工作','type':'input','key':'CaseWork'}]
		}else if (pageType == '规范参与备案') {
			method ='web.INMSpecialComm:SaveStandard'
			//类型^制定/修订^名称^专业组Id^参与人Id^状态^id
			domArr = [{'domId':'StandardType','tip':'请选择类型','key':'StandardType'},
				{'domId':'StandardTypeSub','tip':'请选择制定/修订','key':'StandardTypeSub'},
				{'domId':'StandardDesc','tip':'请输入名称','type':'input','key':'StandardDesc'},
				{'domId':'StandardGroup','tip':'请选择专业组','key':'StandardGroup'},
				{'domId':'StandardParticipant','tip':'请选择参与人','key':'StandardParticipant'}]
		}
		return {'method':method,'parrArr':domArr}
	}
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		if (pageType == '专科活动备案') {
			document.body.classList.add('Activity')
		}else if (pageType == '课题专案备案') {
			document.body.classList.add('Case')
		}else{
			document.body.classList.add('Standard')
		}
		pageData = getSaveParams()
		if (sourceData) { //修改
			updateUI()
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']
		$('#Ward')[0].innerText = perInfo['loginWardDesc']
		$('#Ward')[0].setAttribute('data-id',perInfo['loginWardId'])
		//关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		//参与人
		mui('body').on('tap','.authType',function(){
			blurAllInput()
			var domid = 'Ward'
			var tip = '请先选择病区'
			if (this.id == 'StandardParticipant') {
				domid = 'StandardGroup'
				tip = '请先选择专业组'
			}
			
			var domText = $('#'+domid)[0].innerText
			if (domText == '') {
				mui.toast(tip)
				return
			}
			var dom = this
			setTimeout(function() {
				toChosePerson(dom.id,'多选',domid) //防止切换页面时键盘未隐藏
			}, 100);
			
		});
		//病区列表
		mui('#UL').on('tap','#Ward',downWardListElementTapped);
		//下拉列表
		mui('#UL').on('tap','#level',getCheckLevel);
		mui('#UL').on('tap','.listType',listTypeDomTapped);
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		window.addEventListener('resize',windowResize)
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			unUploadFiles = afterSelectFile('fileUL',unUploadFiles,imgArr)
		});
		
		mui('body').on('hidden','.mui-popover',function(){
			if (this.id != 'optionsPopo') {
				return
			}
			var eleId = mui('#optionsPopo')[0].getAttribute('data-id');
			if (eleId == 'Ward') {
				$('#persons')[0].innerText = ''
				$('#persons')[0].setAttribute('data-id','')
			}else if (eleId == 'StandardGroup'){
				$('#StandardParticipant')[0].innerText = ''
				$('#StandardParticipant')[0].setAttribute('data-id','')
			}
		})
		//专业组
		mui('#UL').on('tap','#StandardGroup',getGroupList);
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-btn',function(){
			var method = getDeleteFileMethod(pageType)
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
	
})(mui)

function selectPerson(domId,params){
	setAuthorDomInfo(domId,params)
}
